package com.company.service;

import com.company.mapper.UserMapper;
import com.company.model.User;
import com.company.utils.DatabaseUtil;
import com.company.utils.MessageResponse;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import javax.crypto.MacSpi;
import javax.servlet.http.HttpSession;

public class LoginService {
    public static SqlSession sqlSession = DatabaseUtil.getSession();
    public static UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    private static String adminName = "admin";
    private static String adminPassword = "123456";

    public static String loginUserKey = "loginUser";

    public static MessageResponse<Boolean> islogin(HttpSession session) {
        return new MessageResponse<>(
                "islogin",
                session.getAttribute(loginUserKey) != null
        );
    }
    public static MessageResponse<String> loginUserName(HttpSession session) {
        User loginUser = (User)session.getAttribute(loginUserKey);
        if(loginUser == null) {
            return new MessageResponse<>(
                    "no login user",
                    null
            );
        } else {
            return new MessageResponse<>(
                    "login user name",
                    loginUser.getName()
            );
        }
    }
    public static MessageResponse<Boolean> loginUser(User user, HttpSession session) {
        User matchUser = userMapper.findOne(user.getName());
        if(matchUser == null) {
            return new MessageResponse<>(
                    "login failed",
                    false
            );
        } else {
            session.setAttribute(loginUserKey, user);
            return new MessageResponse<>(
                    "login success",
                    true
            );
        }
    }

    public static MessageResponse<Boolean> loginAdmin(User user, HttpSession session) {
        if (user.getName().equals(adminName) && user.getPassword().equals(adminPassword)) {
            session.setAttribute(LoginService.loginUserKey, user);
            return new MessageResponse(
                    "login success",
                    true
            );
        } else {
            return new MessageResponse(
                    "login failed",
                    false
            );
        }
    }

    public static MessageResponse<Boolean> exit(HttpSession session) {
        User user = (User)session.getAttribute(loginUserKey);
        if(user == null) {
            return new MessageResponse<>(
                    "exit failed",
                    false
            );
        } else {
            session.removeAttribute(loginUserKey);
            return new MessageResponse<>(
                    "exit success",
                    true
            );
        }
    }


    public static MessageResponse<Boolean> register(User user) {
        User matchUser = userMapper.findOne(user.getName());
        if(matchUser == null) {
            userMapper.insertOne(user);
            return new MessageResponse<>(
                    "register success",
                    true
            );
        } else {
            return new MessageResponse<>(
                    "register failed",
                    false
            );
        }
    }

    public static MessageResponse<Boolean> hasUser(String name) {
        User matchUser = userMapper.findOne(name);
        return new MessageResponse<>(
                "status",
                matchUser != null
        );
    }
}
